"use strict"

function arr(x,y,fn){
    return fn(x)+fn(y);
}
 
let adc = arr(-2,8,Math.abs);
console.log(adc);


function pow(x) {
    return x * x;
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var results = arr.map(pow); 
console.log(results);

let arr1 = [1,2,3,4];
console.log(arr1.reduce(function(x,y){
    return x+y;
}));

var q = [1,5,23,48];
var s = q.filter(function (x){
   return x % 2 === 0;
})
console.log(s);


var arr = ['A', 'B', 'C'];
var r = arr.filter(function (element, index, self) {
    console.log(element); // 依次打印'A', 'B', 'C'
    console.log(index); // 依次打印0, 1, 2
    console.log(self); // c就是变量arr
    return true;
});


var zbc, arr = ['a','b','c','a','c','d'];
zbc = arr.filter(function (element, index, self){
    return self.indexOf(element) === index;
});
console.log(zbc.toString());

 
// 练习：
// 1.利用reduce()求积：
function product(arr) {
    var diyiti=arr.reduce(function(x,y){
        return x*y;
    })
    return diyiti;
}
// 测试:
if (product([1, 2, 3, 4]) === 24 && product([0, 1, 2]) === 0 && product([99, 88, 77, 66]) === 44274384) {
    console.log('测试通过!');
}
else {
    console.log('测试失败!');
}


// 2.不要使用JavaScript内置的parseInt()函数，利用map和reduce操作实现一个string2int()函数:(搞不明白o(￣┰￣*)ゞ)
function string2int(s) {
    var ad = arr.map(function (ss) {
        return ss;
      })
    var dierti = ad.reduce(function (x, y) {
        return x * 10 + y;
    })
    return dierti;
}
// 测试:
if (string2int('0') === 0 && string2int('12345') === 12345 && string2int('12300') === 12300) {
    if (string2int.toString().indexOf('parseInt') !== -1) {
        console.log('请勿使用parseInt()!');
    } else if (string2int.toString().indexOf('Number') !== -1) {
        console.log('请勿使用Number()!');
    } else {
        console.log('测试通过!');
    }
}
else {
    console.log('测试失败!');
}


// 4.小明希望利用map()把字符串变成整数，他写的代码很简洁：
var arr = ['1', '2', '3'];
var r;
r = arr.map(Number);

console.log(r);

